<?php

require_once  __DIR__ . '/includes/authenticate.php';
include_once './db_bb_connect.php';

?>


<div id="viewtrainingftladdercontent">
<h4>View ftladder stats</h4>


<form>
	
<select class="form-control"  name="season" id="select4">
		
<?php

if (isset($_POST['season'])) {

	$setseason = $_POST['season'];

echo  "<option selected=\"selected\" value=\"$setseason\"> $setseason is selected</option>";
	
} else {
	echo  "<option selected=\"selected\" disabled=\"disabled\"> Select season</option>";
}


$sql = "SELECT DISTINCT season 
FROM trainingftladder";

$result = $db->query($sql);

while ($row = $result->fetch_assoc()) {
	$season = $row['season'];

	echo "<option value=\"$season\"> $season </option>";
}

?>
</select>

<br><br>

<?php

if (isset($_POST['season'])) {

    $setseason = $_POST['season'];

?>


 <div class="table-responsive"> 
 <h4>FT ladder stats of <?= $setseason; ?></h4>      
  <table class="table table-hover">
    <thead>
      <tr>
        <th collspan="2">Name</th>
        <th>Result</th>
      </tr>
    </thead>
    <tbody>
      



<?php
	
	$sql3 = "SELECT COUNT(trainingftladder.ftladderresult) AS trainingftladdertot,
	COUNT(case trainingftladder.ftladderresult when 'yes' then 1 else null end) AS trainingftladderyes,  
	users.firstname, 
	users.lastname
	FROM trainingftladder 
	JOIN users 
	ON trainingftladder.playerID = users.user_key
	WHERE trainingftladder.season = '$setseason'
	GROUP BY trainingftladder.playerID
	ORDER BY users.playernumber";

	$result3 = $db->query($sql3);

	while ($row3 = $result3->fetch_assoc()) {
		$firstname = $row3['firstname'];
		$lastname = $row3['lastname'];
		$trainingftladdertot = $row3['trainingftladdertot'];
		$trainingftladderyes = $row3['trainingftladderyes'];

?>
	 <tr>
        <td><?= $firstname ?> <?= $lastname ?> </td>
        <td><?= $trainingftladderyes ?>/<?= $trainingftladdertot ?> </td>
      </tr>

<?php
}

}

?>

    </tbody>
  </table>
  </div>

</div>

<script type="text/javascript">
	
$("#select4").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewftladderstats.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingftladdercontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingftladdercontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

$("#select5").on("change", function(e) {
    postData = $(this.form).serialize();
    $.ajax({
        type: "POST",
        url: "./training_viewftladderstats.php",
        data: postData,
        success: function(data) { 
        	//alert(postData);
            $("#viewtrainingftladdercontent").html(data); 
        },
        error: function (xhr, status, error) {
        	$("#viewtrainingftladdercontent").html(xhr.responseText); 
        }
      }); 
    e.preventDefault();
});  

</script>